//
//  YTModifyPhoneViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/4.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTModifyPhoneViewController.h"
#import "YTUserProfileServices.h"
#import "YTCountryCodeViewController.h"
#import "YTModifyInfoViewController.h"

@interface YTModifyPhoneViewController ()<YTCountryCodeDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem *rightBtn;
@property (weak, nonatomic) IBOutlet UIButton *areaCode;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumText;
@end

@implementation YTModifyPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    @weakify(self);
    [[_phoneNumText.rac_textSignal distinctUntilChanged]
     subscribeNext:^(NSString *text) {
         @strongify(self);
         self.rightBtn.enabled = (text.length > 4);
    }];
    [[_areaCode rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        YTCountryCodeViewController *code = [[YTCountryCodeViewController alloc] init];
        code.delegate = self;
        [self.navigationController pushViewController:code animated:YES];
    }];
}

- (IBAction)saveInfoAction:(id)sender {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"mobile2"] = _phoneNumText.text;
    dict[@"areaCode2"] = [_areaCode.titleLabel.text substringFromIndex:2];
    [SVProgressHUD showWithStatus:@"上传中..."];
    [[YTUserProfileServices saveChangeUserInfo:dict] subscribeNext:^(NSDictionary *response) {
        if ([response[@"resultCode"] integerValue] == 1000) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            for (UIViewController *vc in self.navigationController.viewControllers) {
                if ([vc isKindOfClass:[YTModifyInfoViewController class]]) {
                    [self.navigationController popToViewController:vc animated:YES];
                }
            }
        } else {
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)didSelectWithCountryCode:(NSString *)countryCode {
    [self.areaCode setTitle:[NSString stringWithFormat:@"+ %@", countryCode] forState:UIControlStateNormal];
}

@end
